<?
/**
 * 	Media parser.
 *
 *  The MEDIA class is used for rendering image, stream, files and embedded sources (like Flash).
 *	Note: this class needs some cleaning up, it's one of the Engine's dinosaurs...
 *
 *	@copyright	2010 - oneDot only<br>
 *  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.<br>
 *  The content is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.  see the GNU General Public License for more details.
 *
 *	@author		Koen Betsens <we@onedotonly.be>
 *  @package	Global
 */



class MEDIA{
	
	
	/**
	 * Define defaults
	 *
	 * @name default_sze	default image size 
	 */
	const default_sze	= "120.40";

	/**
	 * Define defaults
	 *
	 * @name default_type	default media type 
	 */
	const default_type	= "media.img";
		
	/**
	 * Notice:
 	 * __construct is halted untill "function MEDIA" is no longer in use.
	 */
	
	
	public function image( $id, $type = self::default_type, $vars = false ) {
	
		if( !$vars->sze ) $vars->sze = self::default_sze;
		if( !$vars->alt ) $vars->alt = $DB->get_key(( int ) $id, $type, "name" );
		
		return "<img src='/public.img?id=$id&sze={$vars->sze}{$vars->getstring}'";
	}
	
	
	
	
	
	
	
	
	/**
 	 * All the latter is deprecated - please don't use!
	 */

	/**
 	 * Part of old MEDIA Class
	 *
	 * @deprecated	search & destroy.
	 */
	function MEDIA($vars=false){
		global $DB, $_init;
		if($vars) $this->_addvars($vars);
		if(isset($this->rate)) $this->rate = $this->_rate();
	}

	/**
 	 * Part of old MEDIA Class
	 *
	 * @deprecated	search & destroy.
	 */
	function _addvars($vars){ if($vars) foreach($vars as $k=>$v) if($k!=$this) $this->$k=$v; }

	/**
 	 * Part of old MEDIA Class
	 *
	 * @deprecated	search & destroy.
	 */
	function _rate(){
		switch($this->rate){
			case "m+":
				return "rate >= (SELECT SUM(rate)/COUNT(*) FROM db_media)";
				break;
				
			case "top+":
				$raw=explode('+',$this->rate);
				return "rate >= (SELECT MIN(rate) FROM db_media ORDER BY rate DESC LIMIT ".$raw[1].")";
				break;
				
			default: return $this->rate;
		}
	
	}

	/**
 	 * Part of old MEDIA Class
	 *
	 * @deprecated	search & destroy.
	 */
	function _img($vars=false){
		if($vars) $this->_addvars($vars);
		if( $vars->ID ) $this->imgid = $vars->ID;
		
		if( is_int( $this->link ) && $Page = new PAGE( $this->link ))
			$this->link = $Page->uri();

		if(!$this->alt) $this->alt = "";
		if(isset($this->imgid)) $this->src = "/public.img?id=".$this->imgid."&sze=".$this->sze.(isset($this->msk)? "&msk=1" :"").(isset($this->unt)? "&unt=1" :"").(isset($this->lay)? "&lay=1" :"").(isset($this->mversion)? "&mversion=".$this->mversion :"");
		else if(isset($this->img)) $this->src = THEME."img/".$this->img;
		$link = ($this->link)? array("<a href=\"$this->link\" " . (( $this->target && $this->target == "blank" ) ? "target=\"_blank\"" : "" ) . ">","</a>") : array(''); 
		
		return $link[0]."<img ".(isset($this->id)? "id='".$this->id."'" :"")." src='".$this->src."' ".(isset($this->style)? "style=\"".$this->style."\"" :"").(isset($this->class)?" class='".$this->class."'" :"")." alt=\"".$this->alt."\" ".(isset($this->left)? "align='left' hspace='2'" :"")." />".$link[1];
	}

	/**
 	 * Part of old MEDIA Class
	 *
	 * @deprecated	search & destroy.
	 */
	function _txt($string){
		global $Engine;
		if(is_int($string)) $string = $Engine->text->get( $string );
		$this->alt = $string;
		$this->src = "/text.img?tpe=".$this->h."&amp;str=".urlencode(($this->upper)? strtoupper(html_entity_decode($string)) : html_entity_decode($string));
		
		return $this->_img();
	}
	
	/**
 	 * Part of old MEDIA Class
	 *
	 * @deprecated	search & destroy.
	 */
	function _flash($vars){
		global $DB;
		$this->_addvars($vars);
		$attr = array();
		
		if($this->src) $this->attr->src = $this->src;
		if(is_int($this->attr->src)) $this->attr->src = "http://".$DB->short('url','db_link',"ID=".$this->attr->src);
		
		$this->attr->style .= "width:". $this->width."px; height:".$this->height."px; ";
		if($this->style)$this->attr->style.= $this->style;
		
		$win = ($this->nameid)? $this->_title((int)$this->nameid) : "";
		if($this->vardb) {
			$this->vardb = str_replace('$oid', $this->objid, $this->vardb);
			$this->attr->flashVars .= "vars=".json_encode(is_string($this->vardb)?
				$DB->tube($this->vardb, true) :
				$DB->gparse($this->vardb[0], $this->vardb[1], $this->vardb[2], $this->vardb[3])
			);
		}

		if(IE){
			$this->attr->name   = $this->attr->id;
			$this->attr->movie  = $this->attr->src;
			$this->attr->width  = $this->width;
			$this->attr->height = $this->height;
			foreach($this->attr as $fk => $fv) $attr[] = "'$fk','$fv'";
			$win .= "<script type='text/javascript'>
				AC_FL_RunContent( 'codebase','http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',".implode(',',$attr).",'quality','best','allowscriptaccess','sameDomain','pluginspage','http://www.macromedia.com/go/getflashplayer');
			</script>";	
		}else{
			$this->attr->src.=".swf";
			foreach($this->attr as $fk => $fv) $attr[] = "$fk ='$fv'";
			$win .= "<embed type='application/x-shockwave-flash' ".implode(' ',$attr)." ></embed>";
		}
		return $win;
	}

}
